<?php
class ScoresController extends AppController {

	var $name = 'Scores';

	function index() {
		$this->Score->recursive = 0;
		$this->set('scores', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid score', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('score', $this->Score->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Score->create();
			if ($this->Score->save($this->data)) {
				$this->Session->setFlash(__('The score has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The score could not be saved. Please, try again.', true));
			}
		}
		$songs = $this->Score->Song->find('list');
		$users = $this->Score->User->find('list');
		$this->set(compact('songs', 'users'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid score', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Score->save($this->data)) {
				$this->Session->setFlash(__('The score has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The score could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Score->read(null, $id);
		}
		$songs = $this->Score->Song->find('list');
		$users = $this->Score->User->find('list');
		$this->set(compact('songs', 'users'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for score', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Score->delete($id)) {
			$this->Session->setFlash(__('Score deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Score was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}

	public function addAjax() {
		if($this->Auth->user()){
			define('DEBUG', 0);
			$this->autoRender = false; 
			$this->Score->create();
			App::import('Model','Song');
			$ActualSong = new Song();
			$user_id = $this->Auth->user('id');
			$song_id = $this->params['form']['song_id'][0];
			$direction = $this->params['form']['direction'];
			$conditions = array('conditions' => array("Score.user_id" => $user_id, "Score.song_id" => $song_id, "Score.direction" => $direction));
			$inserted_scores = $this->Score->find('list',$conditions);
			if (!empty($inserted_scores)){
				CakeLog::write('fraud', "Someone inserting another time to scores! user_id=$user_id, song_id=$song_id, direction=$direction");
			}
			if ($direction =='0'){
				$dir2='1';
				$change='-1';
			} else {
				$dir2='0';
				$change='+1';
			}
			$conditions = array('conditions'=> array("Score.user_id" => $user_id, "Score.song_id" => $song_id, "Score.direction" => $dir2));
			$inserted_diff_scores = $this->Score->find('list',$conditions);
			if (!empty($inserted_diff_scores)){
				$change = 2*$change;
				foreach ($inserted_diff_scores as $id=>$i){
					$this->Score->delete($id);
				}
			}	
			$data= array('Score' => array('id' => null, 'user_id' => $user_id  , 'song_id' => $song_id, 'direction' => $direction, 'date' => null));
			if ($this->Score->save($data)){
				$sql = "update songs set score=score+ $change where id=$song_id";
				$ActualSong->query($sql);
				$scor = $ActualSong->read('score',$song_id);
			} 
				return $scor['Song']['score'];
		} else {
			return 'not logged in';
		}
	}
}
